//
//  DisplayMailView.m
//  OASystem
//
//  Created by macos on 14-4-17.
//  Copyright (c) 2014年 macos. All rights reserved.
//

#import "DisplayMailView.h"

@interface DisplayMailView ()

@end

@implementation DisplayMailView
@synthesize passDisplayMainArr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        if(!passDisplayMainArr)
            passDisplayMainArr = [[NSArray alloc] init];
    }
    return self;
}

//返回上一级
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)replay
{
    WriteEmailView *instance = [[WriteEmailView alloc] init];
    instance.boolDraft = 2;
    instance.draftDic = [NSArray arrayWithArray:passDisplayMainArr];
    [self.navigationController pushViewController:instance animated:YES];
}


- (void)initNavi
{
    //添加导航又边用户信息
    //初始化UINavigation左按钮
    UIButton *helloButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 24)];
    [helloButton setTitle:[NSString stringWithFormat:@"你好，%@",USER_NICK_NAME] forState:UIControlStateNormal];
    helloButton.titleLabel.font = HEIGHT_FONT(13.0f);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:helloButton];
    
    //初始化UINavigation左按钮
    UIButton *naviButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviButton setTitle:@"返回" forState:UIControlStateNormal];
    naviButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviButton];
    
    //初始化UINavigation左按钮
    UIButton *navi2Button = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [navi2Button setTitle:@"回复" forState:UIControlStateNormal];
    navi2Button.titleLabel.font = HEIGHT_FONT(13.0f);
    [navi2Button setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [navi2Button setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [navi2Button addTarget:self action:@selector(replay) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navi2Button];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = NOTICEBOARD_BACKCOLOR;
    [self initNavi];
    [self initContentView];
    
    NSLog(@"passAttackInfoArr--->%@",passDisplayMainArr);
    NSString *hasReadStr = [NSString stringWithFormat:@"%@",[passDisplayMainArr objectAtIndex:17]];
    if(![hasReadStr isEqualToString:@"0"])
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
        //获取附件信息
        [[SendHttpOperation shareSendHttpOperation] GetAttachInfo:@"GetAttachInfo.ashx"
                                                       CliengGuid:[passDisplayMainArr objectAtIndex:22]];
        [SendHttpOperation shareSendHttpOperation].getAttackInfoDelegate = self;
    }
}

- (void)passGetAttackInfo:(NSDictionary *)attackDic
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
    NSLog(@"99-->%@",attackDic);
    
    passAttackInfoArr = [NSArray arrayWithArray:[[[[attackDic objectForKey:@"result"] objectForKey:@"list"] objectForKey:@"data"] objectAtIndex:0]];
    
    //链接标题
    titleAttackLabel.text      = [NSString stringWithFormat:@"%@",[passAttackInfoArr objectAtIndex:2]];
    titleAttackLabel.textColor = NOTICE_CONTENT_BACKCOLOR;
    titleAttackLabel.font      = HEIGHT_FONT(12.0f);
    
    //转换链接大小
    int size                   = [[passAttackInfoArr objectAtIndex:4] intValue]/1000;
    infoLabel.text       = [NSString stringWithFormat:@"%dKB",size];
    infoLabel.font      = NORMAL_FONT(13.0f);
    attackView.hidden = NO;
    
    //设置链接按钮的边框与圆角
    //添加点击事件
    attackView.layer.cornerRadius = 6;
    attackView.layer.masksToBounds = YES;
    [attackView.layer setBorderWidth:1];
    [attackView.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoAttack)];
    [attackView addGestureRecognizer:tap];
}

//调用浏览器打开附件链接
- (void)gotoAttack
{
    NSString *attackUrl = [passAttackInfoArr lastObject];
    
    if([attackUrl hasPrefix:@"/"]||[attackUrl hasPrefix:@" /"])
        attackUrl = [NSString stringWithFormat:@"%@%@",base_url,attackUrl];
    else
        attackUrl = [NSString stringWithFormat:@"%@/%@",base_url,attackUrl];
    
    attackUrl = [attackUrl  stringByReplacingOccurrencesOfString:@" " withString:@""];
    attackUrl = [attackUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:attackUrl]])
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"获取附件失败"}];
}

- (void)initContentView
{
    displayTitleLabel.text      = [NSString stringWithFormat:@"%@",[passDisplayMainArr objectAtIndex:10]];
    displayTitleLabel.textColor = NOTICE_CONTENT_BACKCOLOR;
    displayTitleLabel.font      = HEIGHT_FONT(18.0f);
    
    
    //获取html内容进行转换
    NSString *contentStr = [self convertHtmlStr:[passDisplayMainArr objectAtIndex:11]];
    diaplayWeb.opaque=NO;
    diaplayWeb.backgroundColor = [UIColor clearColor];
    diaplayWeb.scrollView.showsHorizontalScrollIndicator = NO;
    [diaplayWeb loadHTMLString:contentStr baseURL:nil];
    
    //是否存在附件
    NSString *hasAttack   = [NSString stringWithFormat:@"%@",[passDisplayMainArr objectAtIndex:17]];
    attackView.hidden     = [hasAttack isEqualToString:@"1"]?NO:YES;
    
    //发送时间
    NSString *sendTimeStr = [NSString stringWithFormat:@"%@",[passDisplayMainArr objectAtIndex:13]];
    sendTime.text         = sendTimeStr;
    sendTime.textColor    = NOTICE_CONTENT_WORD_COLOR;
    sendTime.font         = NORMAL_FONT(11.0f);
}

//对html设置字体以及大小
- (NSString *)convertHtmlStr:(NSString *)paramSTr
{
    NSString *resultStr = [paramSTr stringByReplacingOccurrencesOfString :@"{^^^}" withString:@"\n"];
    resultStr = [resultStr stringByReplacingOccurrencesOfString :@"{###}" withString:@"\r"];
    //建立格式
    resultStr           = [resultStr stringByAppendingString:@"</div>"];
    NSString *tempStr   = [NSString stringWithFormat:@"<div style=\"font-size:12\" style=\"color:#2c2c2c\">%@",resultStr];
    
    return tempStr;
}


#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    
    //自定义分割线
    UIImageView *line    = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 1)];
    line.backgroundColor = NOTICEBOARD_LINE_COLOR;
    [cell.contentView addSubview:line];
    

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 11, 75, 21)];
    label.text     = indexPath.row == 0?@"发件人：":(indexPath.row == 1?@"收件人：":@"抄  送：");
    label.font     = HEIGHT_FONT(14.0f);
    [cell.contentView addSubview:label];
    
    NSString *str              = [passDisplayMainArr objectAtIndex:indexPath.row == 0?3:(indexPath.row==1?5:9)];
    UILabel *contentLabel      = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 210, 35)];
    contentLabel.numberOfLines = str.length>20?2:0;
    contentLabel.text          = str;
    contentLabel.font          = NORMAL_FONT(14.0f);
    [cell.contentView addSubview:contentLabel];
    
    cell.contentView.backgroundColor = NOTICEBOARD_BACKCOLOR;
    cell.selectionStyle              = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
